package com.boco.modules.fdoc.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.boco.common.constants.BusinessConstants;
import com.boco.common.json.BaseJsonVo;
import com.boco.common.utils.JsonUtils;
import com.boco.modules.fdoc.dao.BookingDao;
import com.boco.modules.fdoc.dao.DeptDao;
import com.boco.modules.fdoc.dao.DoctorCommentDao;
import com.boco.modules.fdoc.dao.DoctorDao;
import com.boco.modules.fdoc.dao.HospitalDao;
import com.boco.modules.fdoc.dao.ScheDao;
import com.boco.modules.fdoc.model.DoctorCommentEntity;
import com.boco.modules.fdoc.model.DoctorDetailScheCommEntity;
import com.boco.modules.fdoc.model.DoctorEntity;
import com.boco.modules.fdoc.model.ScheduleEntity;
import com.boco.modules.fdoc.service.DocService;
import com.boco.modules.fdoc.vo.DoctorDetailVo;
import com.boco.modules.fdoc.vo.QuickBookingVo;
import com.boco.modules.fdoc.vo.ScheduleVo;

@Service

public class DocServiceImpl implements DocService {

	@Resource
	DoctorDao doctorDao;
	@Resource
	ScheDao scheDao;
	@Resource
	DoctorCommentDao doctorCommentDao;
	@Resource
	BookingDao bookDao;
	@Resource
	HospitalDao hospitalDao;
	@Resource
	DeptDao deptDao;

	@Override
	public List<DoctorEntity> getDoctorList(DoctorEntity doctor) {
		return doctorDao.getList(doctor);
	}

	@Override
	public DoctorDetailVo getDoctorDetail(int id) {
		DoctorEntity doc = doctorDao.getDetail(id);
		/**
		 * 
		 注释部分为医生详情整合版本，返回医生详情、相关评论、排版等全部信息
			String sid = String.valueOf(id);
			List<DoctorCommentEntity> comm = doctorCommentDao.getByDocId(sid);
			List<ScheduleEntity> sche = scheDao.findScheList(id);
			for (ScheduleEntity sc : sche) {
				SimpleDateFormat workDate = new SimpleDateFormat("MM/dd");
				sc.setWorkDate(workDate.format(sc.getWorkTime()));
				String[] weekDays = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
				Calendar weekDate = Calendar.getInstance();
				weekDate.setTime(sc.getWorkTime());
				int w = weekDate.get(Calendar.DAY_OF_WEEK) - 1;
				if (w < 0)
					w = 0;
				sc.setWeekDate(weekDays[w]);
				sc.setWorkTime(null);
			}
			DoctorDetailScheCommEntity ddsc = new DoctorDetailScheCommEntity();
			ddsc.setComm(comm);
			ddsc.setSche(sche);
			ddsc.setDoc(doc);
		*/
		DoctorDetailVo returnVo = new DoctorDetailVo();
		BeanUtils.copyProperties(doc, returnVo);
		returnVo.setAllEvaluateNum(doctorCommentDao.getCommentCount(id));
		
		DoctorDetailVo paramVo = new DoctorDetailVo();
		paramVo.setId(id);
		paramVo.setHisId(returnVo.getHisId());
		paramVo.setCommentLevel(BusinessConstants.GOOD_EVALUATE);
		int goodNum = doctorCommentDao.getCommentLevelsCount(paramVo);
		paramVo.setCommentLevel(BusinessConstants.GENERAL_EVALUATE);
		int generalNum = doctorCommentDao.getCommentLevelsCount(paramVo);
		paramVo.setCommentLevel(BusinessConstants.BAD_EVALUATE);
		int badNum = doctorCommentDao.getCommentLevelsCount(paramVo);
		double goodComment = 0;
		if (returnVo.getAllEvaluateNum()!=0) {
			goodComment = (double)goodNum/returnVo.getAllEvaluateNum();
		}
		BigDecimal b = new BigDecimal(goodComment); 
		goodComment = b.setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue()*100;  
		
		returnVo.setHospName(hospitalDao.get(returnVo.getHospId()).getName());
		returnVo.setDeptName(deptDao.getDeptDetail(returnVo.getDeptId()).getName());
		returnVo.setBookingNum(bookDao.getCountByDocId(id));
		returnVo.setGoodEvaluateNum(goodNum);
		returnVo.setGeneralEvaluateNum(generalNum);
		returnVo.setBadEvaluateNum(badNum);
		returnVo.setGoodComment(goodComment);
		return returnVo;
	}

	@Override
	public BaseJsonVo getDocByName(String name) {
		BaseJsonVo vo = new BaseJsonVo();
		vo.setData(doctorDao.getDocByName(name));
		return vo;
	}

	@Override
	public List<DoctorEntity> getDefaultDocs(String city) {
		return doctorDao.getDefaultDocs(city);
	}

	@Override
	public List<ScheduleEntity> getDocSchedual(ScheduleVo vo) {
		List<ScheduleEntity> sche = scheDao.findScheList(vo);
		for (ScheduleEntity sc : sche) {
			SimpleDateFormat workDate = new SimpleDateFormat("MM/dd");
			sc.setWorkDate(workDate.format(sc.getWorkTime()));
			String[] weekDays = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
			Calendar weekDate = Calendar.getInstance();
			weekDate.setTime(sc.getWorkTime());
			int w = weekDate.get(Calendar.DAY_OF_WEEK) - 1;
			if (w < 0)
				w = 0;
			sc.setWeekDate(weekDays[w]);
			sc.setWorkTime(null);
		}
		return sche;
	}

	@Override
	public List<DoctorCommentEntity> getSingleDocComment(DoctorDetailVo vo) {
		
		return doctorCommentDao.getSingleDocComment(vo);
	}

	@Override
	public List<DoctorEntity> getBookingDoctor(QuickBookingVo vo) {
		
		return doctorDao.getBookingDoctor(vo);
	}

	@Override
	public List<DoctorEntity> getDoctorsByCityAndDept(String city, int deptId) {
		return doctorDao.getDoctorsByCityAndDept(city, deptId);
	}

	@Override
	public DoctorEntity getDocByDocUid(String docUid) {
		return doctorDao.getDocByDocUid(docUid);
	}

}
